﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using System.Windows.Forms;

namespace sands
{
    public class SandsForm : Form
    {
        protected System.Windows.Forms.Control ctlMums=null;
        protected System.Windows.Forms.Control ctlHome=null;
        protected System.Windows.Forms.Control ctlMobile=null;
        public System.Windows.Forms.Control ctlIndex=null;
        protected System.Windows.Forms.Control ctlZipCity=null;
        protected System.Windows.Forms.Control ctlSurname=null;
        protected System.Windows.Forms.Control ctlFName=null;
        protected System.Windows.Forms.Control ctlStreet=null;
        protected System.Windows.Forms.Control ctlDads=null;
        protected System.Windows.Forms.Control ctlTuition=null;
        protected System.Windows.Forms.DateTimePicker ctlDate=null;
        protected System.Windows.Forms.RadioButton rbAdult=null;
        protected System.Windows.Forms.RadioButton rbMatura=null;
        protected System.Windows.Forms.RadioButton rbChild = null;
        protected System.Windows.Forms.RadioButton rbDiscount0=null;
        protected System.Windows.Forms.RadioButton rbDiscount10 = null;
        protected System.Windows.Forms.RadioButton rbDiscount20 = null;
        protected System.Windows.Forms.RadioButton rbDiscount30 = null;
        protected System.Windows.Forms.CheckBox cbRate=null;
        protected System.Windows.Forms.CheckBox cbTuition=null;

        public Student StudentDetails
        {
            get
            {
                if (!this.DesignMode)
                {
                    Student res = new Student(ctlIndex.Text, ctlSurname.Text, ctlFName.Text);

                    res.Street = ctlStreet.Text;
                    res.ZipCity = ctlZipCity.Text;
                    res.MobilePhone = ctlMobile.Text;
                    res.DadsPhone = ctlDads.Text;
                    res.HomePhone = ctlHome.Text;
                    res.MumsPhone = ctlMums.Text;
                    res.StartDate = ctlDate.Value.ToShortDateString();

                    res.isAdult = rbAdult.Checked;
                    res.isMatura = rbMatura.Checked;
                    res.hasRates = cbRate.Checked;

                    res.Discount = rbDiscount30.Checked ? "30" : rbDiscount20.Checked ? "20" : rbDiscount10.Checked ? "10" : "0";
                    
                    if (cbTuition.Checked)
                        res.NewTuition = ctlTuition.Text;
                    else
                        res.NewTuition = String.Empty;

                    return res;
                }
                else
                    return null;
            }
            set
            {
                if (!this.DesignMode)
                {
                    ctlIndex.Text = value.Index;
                    ctlSurname.Text = value.Surname;
                    ctlFName.Text = value.FirstName;
                    ctlStreet.Text = value.Street;
                    ctlZipCity.Text = value.ZipCity;
                    ctlMobile.Text = value.MobilePhone;
                    ctlDads.Text = value.DadsPhone;
                    ctlHome.Text = value.HomePhone;
                    ctlMums.Text = value.MumsPhone;
                    try
                    {
                        ctlDate.Value = DateTime.Parse(value.StartDate);
                    }
                    catch (ArgumentOutOfRangeException)
                    {
                        MessageBox.Show("Data rozpoczęcia kursu została zaktualizowana! Proszę sprawdź czy jest ona poprawna", "Aktualizacja", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        ctlDate.Value = DateTime.Parse(value.StartDate).AddYears(1);
                    }

                    rbAdult.Checked = value.isAdult;
                    rbMatura.Checked = value.isMatura;
                    cbRate.Checked = value.hasRates;
                    rbChild.Checked = rbMatura.Checked ? false : cbRate.Checked ? !value.isAdult : false;

                    rbDiscount0.Checked = false;
                    rbDiscount10.Checked = false;
                    rbDiscount20.Checked = false;
                    rbDiscount30.Checked = false;

                    switch (value.Discount)
                    {
                        case "1":
                            rbDiscount0.Checked = true;
                            break;
                        case "0,9":
                            rbDiscount10.Checked = true;
                            break;
                        case "0,81":
                            rbDiscount20.Checked = true;
                            break;
                        case "0,729":
                            rbDiscount30.Checked = true;
                            break;
                    }

                    int tmp=0;
                    if (int.TryParse(value.NewTuition, out tmp))
                    {
                        cbTuition.Checked = true;
                        ctlTuition.Text = tmp.ToString();
                    }
                    else
                        cbTuition.Checked = false;
                }
            }
        }       
    }
}
